home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 October / Enter 10 2006.iso / Uzytki / Spiceworks Desktop 0.8.34 Beta / Spiceworks.exe / ruby / bin / generate_yaml_index.rb < prev    next >
Encoding:
Ruby Source  |  2006-05-26  |  1.9 KB  |  79 lines

  1. #!/usr/bin/env ruby
  2.  
  3. # Generate the yaml/yaml.Z index files for a gem server directory.
  4. #
  5. # Usage:  generate_yaml_index.rb --dir DIR [--verbose]
  6.  
  7. $:.unshift '~/rubygems' if File.exist? "~/rubygems"
  8.  
  9. require 'optparse'
  10. require 'rubygems'
  11. require 'zlib'
  12.  
  13. Gem.manage_gems
  14.  
  15. class Indexer
  16.  
  17.   def initialize(options)
  18.     @options = options
  19.     @directory = options[:directory]
  20.   end
  21.  
  22.   def gem_file_list
  23.     Dir.glob(File.join(@directory, "gems", "*.gem"))
  24.   end
  25.  
  26.   def build_index
  27.     build_uncompressed_index
  28.     build_compressed_index
  29.   end
  30.   
  31.   def build_uncompressed_index
  32.     puts "Building yaml file" if @options[:verbose]
  33.     File.open(File.join(@directory, "yaml"), "w") do |file|
  34.       file.puts "--- !ruby/object:Gem::Cache"
  35.       file.puts "gems:"
  36.       gem_file_list.each do |gemfile|
  37.         spec = Gem::Format.from_file_by_path(gemfile).spec
  38.     puts "   ... adding #{spec.full_name}" if @options[:verbose]
  39.         file.puts "  #{spec.full_name}: #{spec.to_yaml.gsub(/\n/, "\n    ")[4..-1]}"
  40.       end
  41.     end
  42.   end
  43.  
  44.   def build_compressed_index
  45.     puts "Building yaml.Z file" if @options[:verbose]
  46.     File.open(File.join(@directory, "yaml.Z"), "w") do |file|
  47.       file.write(Zlib::Deflate.deflate(File.read(File.join(@directory, "yaml"))))
  48.     end
  49.   end
  50. end
  51.  
  52.  
  53. options = {
  54.   :directory => '.',
  55.   :verbose => false,
  56. }
  57.  
  58. ARGV.options do |opts|
  59.   opts.on_tail("--help", "show this message") {puts opts; exit}
  60.   opts.on('-d', '--dir=DIRNAME', "base directory containing gems subdirectory", String) do |value|
  61.     options[:directory] = value
  62.   end
  63.   opts.on('-v', '--verbose', "show verbose output") do |value|
  64.     options[:verbose] = value
  65.   end
  66.   opts.parse!
  67. end
  68.  
  69. if options[:directory].nil?
  70.   puts "Error, must specify directory name. Use --help"
  71.   exit
  72. elsif ! File.exist?(options[:directory]) ||
  73.     ! File.directory?(options[:directory])
  74.   puts "Error, unknown directory name #{directory}."
  75.   exit
  76. end
  77.  
  78. Indexer.new(options).build_index
  79.